/**
 * @program: pettyloan
 * @ClassName OverDueController
 * @author: 胡林
 * @create: 2022-12-12 16:37
 **/

package com.wn.pettyloan.controller;

import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.pettyloan.dto.OverDueDto;
import com.wn.pettyloan.dto.PayDto;
import com.wn.pettyloan.dto.RepayRecordDto;
import com.wn.pettyloan.entity.Echarts;
import com.wn.pettyloan.entity.Overdue;
import com.wn.pettyloan.entity.PayType;
import com.wn.pettyloan.service.OverdueService;
import com.wn.pettyloan.service.PayTypeService;
import com.wn.pettyloan.service.RepayRecordService;
import com.wn.pettyloan.util.Result;


import org.springframework.transaction.annotation.Transactional;

import com.wn.pettyloan.vo.OverdueVo2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("overDue")
public class OverDueController {

    @Resource
    private OverdueService overdueService;
    @Resource
    private PayTypeService payTypeService;
    @Resource
    private RepayRecordService repayRecordService;

    @PostMapping("selOverDue")
    public Result selOverDue(@RequestBody OverDueDto overDueDto) {

        List<Overdue> overdues = overdueService.selOverDueByUserId(overDueDto.getUserId());
        if (overdues != null) {
            return Result.ok().data("overDueList", overdues);
        } else {
            return Result.error();
        }
    }

    @PostMapping("selOne")
    public Result selOne(@RequestBody OverDueDto overDueDto) {
        Overdue overdue = overdueService.selectOne(overDueDto.getOverdueId());
        if (overdue == null) {
            return Result.error();
        }
        List<PayType> payTypes = payTypeService.selAllPayType();
        Integer repayRecordId = overdue.getRepayRecordId();
        return Result.ok().data("overDueInfo", overdue).data("payTypeList", payTypes).data("repayRecordId", repayRecordId);
    }

    @Transactional
    @PostMapping("overDuePay")
    public Result overDuePay(@RequestBody OverDueDto overDueDto) {
        Overdue overdue = overdueService.selectOne(overDueDto.getOverdueId());
        if (overdue == null) {
            return Result.error();
        }
        overdue.setOverdueStatus(2);
        overdueService.updateByPrimaryKeySelective(overdue);
        PayDto payDto = new PayDto();
        payDto.setPayTypeId(overDueDto.getPayTypeId());
        payDto.setRepayReocrdId(overDueDto.getRepayReocrdId());
        if (repayRecordService.paySuccess(payDto)) {
            return Result.ok();
        }
        return Result.error();
    }

    @PostMapping("empOverdue")
    public Result empOverdue(@RequestBody RepayRecordDto repayRecordDto) {
        PageHelper.startPage(repayRecordDto.getCurrentPage(), repayRecordDto.getPageSize(), true);
        List<OverdueVo2> overdueVo2s = overdueService.empOverdue();
        PageInfo<OverdueVo2> overdueVo2PageInfo = new PageInfo<>(overdueVo2s);
        if (overdueVo2s != null) {
            return Result.ok().data("list", overdueVo2s).data("total", overdueVo2PageInfo.getTotal());
        } else {
            return Result.error();
        }
    }

    //借款全况表

    @PostMapping("seleChart")
    public Result seleChart() {
        List<Echarts> echarts = overdueService.seleChart();
        if(echarts!=null){
            String[] names = {"已还款","进行中","已逾期"};
            for (Echarts e:echarts) {
                e.setName(names[e.getStatus() - 1]);
            }
            return Result.ok().data("echartsList", echarts);
        }else {
            return Result.error();
        }

    }
}
